import axios from 'axios';
axios.default.timeout = 6000;
axios.default.baseURL = 'https://apif.java.crmeb.net';

let token = localStorage.getItem('token')
// interceptors 拦截   请求拦截
axios.interceptor.request.use((config) => {
    // console.log(config);
    // 添加请求头
    config.headers['authori-zation'] = token;

    return config

}, (err) => {
    console.log('拦截失败', err);
})
// 响应拦截
axios.interceptor.response.use((res) => {
    console.log(res);

    console.log('拦截成功');
    if (res.data.code === 401) {
        alert('token过期无权限')
        return
    }
    return res
}, (err) => {
    console.log('拦截失败', err);
})

let AxiosHttp = (url, method, data = null) => {
    return axios({
        method,
        url,
        params: method === 'get' ? data : null,
        data: method === 'post' ? data : null
    })
}
export default AxiosHttp;